home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Environments / Clean 1.2.4 / IO Examples / Worm / wormstate.icl < prev   
Text File  |  1997-05-01  |  3KB  |  146 lines

  1. implementation module wormstate
  2.  
  3. import    StdInt, StdBool, StdTuple, StdList, StdEnum
  4. from    deltaTimer        import TicksPerSecond
  5. from    deltaPicture    import Point, Rectangle
  6. from    deltaSystem        import RightKey
  7. import    Highscore, Random
  8.  
  9. //    The worm data types.
  10.  
  11. ::    *State
  12.     =    {    level        :: Level
  13.         ,    food        :: Food
  14.         ,    foodsupply    :: [Food]
  15.         ,    grow        :: Grow
  16.         ,    points        :: Points
  17.         ,    dir            :: Direction
  18.         ,    worm        :: Worm
  19.         ,    best        :: HiScores
  20.         ,    lives        :: Lives
  21.         }
  22. ::    Level
  23.     =    {    fix            :: Int
  24.         ,    speed        :: Int
  25.         ,    level        :: Int
  26.         ,    obstacles    :: [Obstacle]
  27.         }
  28. ::    Food
  29.     =    {    value        :: Int
  30.         ,    pos            :: Point
  31.         }
  32. ::    Grow        :== Int
  33. ::    Direction    :== Char
  34. ::    Obstacle    :== Rectangle
  35. ::    Segment        :== Point
  36. ::    Worm        :== [Segment]
  37. ::    Points        :== Int
  38. ::    Lives        :== Int
  39.  
  40. SizeX            :== 45
  41. SizeY            :== 26
  42.  
  43. NrOfWorms        :== 4
  44. NrOfLevels        :== 8
  45. PointsPerLevel    :== 500
  46. StartLevel        :== 0
  47.  
  48. EasySpeed        :== TicksPerSecond/6
  49. MediumSpeed        :== TicksPerSecond/9
  50. HardSpeed        :== TicksPerSecond/18
  51. Accelation        :==    TicksPerSecond/18
  52.  
  53.  
  54. //    Initial State.
  55. InitState :: HiScores -> State
  56. InitState best
  57. =    {    level        = initlevel
  58.     ,    food        = initfood
  59.     ,    foodsupply    = []
  60.     ,    grow        = 0
  61.     ,    points        = 0
  62.     ,    dir            = RightKey
  63.     ,    worm        = initworm
  64.     ,    best        = best
  65.     ,    lives        = NrOfWorms
  66.     }
  67. where
  68.     initfood        = {value=0,pos=(-10,-10)}
  69.     initlevel        = InitLevel EasySpeed
  70.     initworm        = NewWorm initlevel
  71.  
  72.  
  73. //    Make a new initial worm.
  74. NewWorm :: Level -> Worm
  75. NewWorm {Level | level}
  76. =    [(x,y)\\x<-[5,4..1]]
  77. where
  78.     y    = StartHeights!!(level mod NrOfLevels)
  79.  
  80. StartHeights :: [Int]
  81. StartHeights =: [13,5,13,13,13,1,1,14]
  82.  
  83.  
  84. //    Construct the next level.
  85. InitLevel :: Int -> Level
  86. InitLevel fix
  87. =    {fix=fix,speed=fix,level=StartLevel,obstacles=Obstacles!!StartLevel}
  88.  
  89. DecreaseLevel :: Level -> Level
  90. DecreaseLevel curlevel=:{speed,level}
  91. #    level    = level-1
  92.     speed    = if (level mod NrOfLevels==0 && level<>0) (speed+Accelation) speed
  93. =    {    curlevel &    fix            = speed
  94.                  ,    speed        = speed
  95.                  ,    level        = level
  96.                  ,    obstacles    = Obstacles!!(level mod NrOfLevels)
  97.     }
  98.  
  99. IncreaseLevel :: Level -> Level
  100. IncreaseLevel curlevel=:{speed,level}
  101. #    speed    = if (level mod NrOfLevels==0 && level<>0) (speed-Accelation) speed
  102.     level    = level+1
  103. =    {    curlevel &    fix            = speed
  104.                  ,    speed        = speed
  105.                  ,    level        = level
  106.                  ,    obstacles    = Obstacles!!(level mod NrOfLevels)
  107.     }
  108.  
  109. Obstacles :: [[Obstacle]]
  110. Obstacles =: [    []
  111.              ,    [((12,11),(34,16))]
  112.              ,    [((12, 1),(34, 3)), ((12,24),(34,26))]
  113.              ,    [(( 7, 7),(38, 9)), (( 7,17),(38,19))]
  114.              ,    [(( 1, 1),(18,10)), ((28,17),(45,26))]
  115.              ,    [((14, 3),(15,24)), ((30, 3),(31,24))]
  116.              ,    [(( 3,13),(43,14)), ((22, 3),(24,24))]
  117.              ,    [(( 3, 3),(20,12)), ((26,15),(43,24))]
  118.              ]
  119.  
  120.  
  121.  
  122. //    Generate a food supply.
  123. FoodSupply :: RandomSeed -> [Food]
  124. FoodSupply seed
  125. =    [{value=value,pos=pos}:FoodSupply seed3]
  126. where
  127.     (random1,seed1)    = Random seed
  128.     (random2,seed2)    = Random seed1
  129.     (random3,seed3)    = Random seed2
  130.     foodx            = (IncMod random2 (SizeX-2))+1
  131.     foody            = (IncMod random3 (SizeY-2))+1
  132.     pos                = (foodx,foody)
  133.     value            = IncMod random1 9
  134.     
  135.     IncMod a b        = (a mod b)+1
  136.  
  137. //    Think of some new random food.
  138. NewFood :: Worm Level [Food] -> (Food, [Food])
  139. NewFood worm level=:{obstacles} [food=:{pos}:foods]
  140. |    isMember pos worm || any (InRectangle pos) obstacles    = NewFood worm level foods
  141. |    otherwise                                                = (food, foods)
  142. where
  143.     InRectangle :: Point Obstacle -> Bool
  144.     InRectangle (x,y) ((lx,ty),(rx,by))
  145.     =    x>=lx && x<=rx && y>=ty && y<=by
  146.